package GUI;

import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.UnicodeFont;
import org.newdawn.slick.font.effects.ColorEffect;
import org.newdawn.slick.geom.Vector2f;

import Game.GameHelper;
import Game.Buff.BuffBase;
import Game.Objects.GOPlayer;
import Screens.MyScreens.GameScreen;


/** GUI Player info class
 * @author Aikidos
 */
public class GuiPlayerInfo {
	
	/** Standard font */
	private UnicodeFont _fontStandard;
	
	/** Cyan font */
	private UnicodeFont _fontCyan;
	
	/** Yellow font */
	private UnicodeFont _fontYellow;
	
	/** Red font */
	private UnicodeFont _fontRed;
	
	/** Background HP bar sprite */
	private Image _spriteHPBack;
	
	/** Background EXP bar sprite */
	private Image _spriteExpBack;
	
	/** Foreground HP bar sprite */
	private Image _spriteHPForeg;
	
	/** Foreground EXP bar sprite */
	private Image _spriteExpForeg;
	
	/** Player */
	private GOPlayer _player;

	public GuiPlayerInfo(GOPlayer player) throws SlickException {
		this._spriteHPBack = new Image("res/gfx/gui_health_bar_back.png");
		this._spriteExpBack = new Image("res/gfx/gui_exp_bar_back.png");
		this._spriteHPForeg = new Image("res/gfx/gui_health_bar_foreg.png");
		this._spriteExpForeg = new Image("res/gfx/gui_exp_bar_foreg.png");
		this._player = player;
		
		// Initialize fonts
		_fontStandard = new UnicodeFont("res/fonts/mainfont.ttf", 8, false, false);
		_fontStandard.addAsciiGlyphs();
		_fontStandard.getEffects().add(new ColorEffect(java.awt.Color.WHITE));
		_fontStandard.loadGlyphs();
		
		_fontCyan = new UnicodeFont("res/fonts/mainfont.ttf", 8, false, false);
		_fontCyan.addAsciiGlyphs();
		_fontCyan.getEffects().add(new ColorEffect(java.awt.Color.CYAN));
		_fontCyan.loadGlyphs();
		
		_fontYellow = new UnicodeFont("res/fonts/mainfont.ttf", 8, false, false);
		_fontYellow.addAsciiGlyphs();
		_fontYellow.getEffects().add(new ColorEffect(java.awt.Color.YELLOW));
		_fontYellow.loadGlyphs();
		
		_fontRed = new UnicodeFont("res/fonts/mainfont.ttf", 8, false, false);
		_fontRed.addAsciiGlyphs();
		_fontRed.getEffects().add(new ColorEffect(java.awt.Color.RED));
		_fontRed.loadGlyphs();
	}
	
	/** Returns background HP bar sprite
	 * @return Background HP bar sprite
	 */
	public Image getBackground() {
		return _spriteHPBack;
	}
	
	/** Returns foreground HP bar sprite
	 * @return Foreground HP bar sprite
	 */
	public Image getForeground() {
		return _spriteHPForeg;
	}
	
	/** Draw player info
	 * @param g - Slick2d graphics
	 */
	public void draw(Graphics g) {
		// Draw text
		_fontStandard.drawString(10, 10, "HP:");
		_fontStandard.drawString(10, 30, "EXP:");
		
		// Draw background
		_spriteHPBack.draw(45, 12);
		_spriteExpBack.draw(45, 32);
		
		// Draw hp
		float hpBarWidth = ((float) _spriteHPForeg.getWidth() / _player.MaxHealth) * _player.getHealth();
		_spriteHPForeg.draw(45, 12, hpBarWidth, _spriteHPForeg.getHeight());
		
		// Draw exp
		float hpExpWidth = ((float) _spriteExpForeg.getWidth() / _player.getExpMax()) * _player.getExp();
		_spriteExpForeg.draw(45, 32, hpExpWidth, _spriteExpForeg.getHeight());
		
		// Draw bar's lines
		g.setColor(Color.black);
		g.drawLine(45 + hpBarWidth - 1, 14, 45 + hpBarWidth - 1, 12 + _spriteHPForeg.getHeight());
		g.drawLine(45 + hpExpWidth - 1, 34, 45 + hpExpWidth - 1, 32 + _spriteExpForeg.getHeight());
		
		// Draw level
		_fontStandard.drawString(10, 55, "Level: " + _player.getLvL());
		
		// Draw damage
		_fontStandard.drawString(10, 75, "Damage: " + _player.MinDamage + " - " + _player.MaxDamage);
		
		// Draw defense
		_fontStandard.drawString(10, 95, "Defense: " + _player.Defense);
		
		// Draw magic
		_fontCyan.drawString(10, 120, "Magic: " + _player.Magic);
		
		// Draw hunger
		int hungerPercent = _player.getHunger() / 10;
		UnicodeFont drawHungerFont = _fontStandard;
		if ((hungerPercent >= 50) && (hungerPercent < 85)) drawHungerFont = _fontYellow;
		else if (hungerPercent >=85) drawHungerFont = _fontRed;
		drawHungerFont.drawString(10, 140, "Hunger: " + hungerPercent + "%");
		
		// Draw gold
		_fontYellow.drawString(10, 165, "Gold: " + _player.Gold + "g");
		
		// Draw stage
		String stageText = "Stage: " + (((GameScreen)_player.getObjectManager().getLevel().getScreen()).getFloor() + 1);
		_fontStandard.drawString(
				(GameHelper.getGame().getWidth() - _fontStandard.getWidth(stageText)) - 15, 
				(GameHelper.getGame().getHeight() - _fontStandard.getHeight(stageText)) - 15, stageText);
		
		// Draw buff icons and text
		Vector2f drawBuffIconPos = new Vector2f(180, 10);
		for (BuffBase buff : _player.getBuffManager().getList()) {
			if (!_player.getBuffManager().isRemoved(buff)) {
				buff.drawIcon(g, drawBuffIconPos);

				String s = (buff.isUnlimited()) ? "?" : String.valueOf(buff.getNumOfSteps());
				_fontYellow.drawString(
						(drawBuffIconPos.x + 16) - _fontYellow.getWidth(s) / 2,
						drawBuffIconPos.y + 40, s);

				drawBuffIconPos.x += 42;
			}
		}
    }
}
